<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
          type="danger"
          size="small"
          plain
          v-if="permission.socialhousing_settlement"
          @click="handleSettlement"
          >月结
        </el-button>
        <el-button
          type="danger"
          size="small"
          plain
          v-if="permission.socialhousing_nosettlement"
          @click="handleNoSettlement"
          >反月结
        </el-button>
        <el-button
          type="danger"
          size="small"
          plain
          v-if="permission.socialhousing_import"
          @click="handleImport"
          >数据导入
        </el-button>
        <el-button
          type="danger"
          size="small"
          plain
          v-if="permission.socialhousing_export"
          @click="handleExport"
          >数据导出
        </el-button>
        <template slot="deptId" slot-scope="{ row }">
          <div>{{ row.deptName }}</div>
        </template>
      </template>
    </avue-crud>
    <el-dialog
      title="反月结"
      append-to-body
      :visible.sync="nosettlementBox"
      width="345px"
    >
      <div v-if="nosettlementBox">
        <div class="label">选择会计期间</div>
        <div class="date">
          <el-date-picker
            v-model="accountPeriod1"
            type="month"
            placeholder="选择月"
            format="yyyy-MM"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </div>
        <div class="tip">注：反月结后，目标会计期间月结数据会被清空！</div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="nosettlementBox = false">取 消</el-button>
          <el-button type="primary" @click="createSettlementBox"
            >确 定</el-button
          >
        </span>
      </div>
    </el-dialog>
    <el-dialog
      title="社保公积金数据导入"
      append-to-body
      :visible.sync="excelBox"
      width="555px"
    >
      <avue-form
        v-if="excelBox"
        :option="excelOption"
        v-model="excelForm"
        :upload-before="uploadBefore"
        :upload-after="uploadAfter"
      >
        <template slot="excelTemplate">
          <div>
            <el-button type="primary" @click="handleTemplate">
              下载模板<i class="el-icon-download el-icon--right"></i>
            </el-button>
            <div class="downTip">
              说明：只有基于模版导入才可确保数据的正确性，如之前应用过标准模版，可忽略第一步直接导入。
            </div>
          </div>
        </template>
        <template slot="excelFile">
          <el-upload
            class="upload-demo"
            :show-file-list="false"
            :before-upload="uploadBefore"
            drag
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或<em>点击上传</em>
            </div>
            <div class="el-upload__tip" slot="tip">
              注：导入的数据会替换当前页面数据
            </div>
          </el-upload>
        </template>
      </avue-form>
    </el-dialog>
  </basic-container>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove,
  settlement,
  nosettlement,
  importWages,
} from "@/api/wages/socialhousing";
import { mapGetters } from "vuex";
import { getDeptTree } from "@/api/system/dept";
import { getToken } from "@/util/auth";
export default {
  data() {
    return {
      showTable: false,
      accountPeriod: "",
      excelBox: false,
      nosettlementBox: false,
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "第一步",
            prop: "excelTemplate",
            formslot: true,
            span: 24,
            // tipPlacement: "注：导入的数据会替换当前页面数据",
            // tip: "注：导入的数据会替换当前页面数据",
            // labelTip: "注：导入的数据会替换当前页面数据",
            // labelTipPlacement: "123",
          },
          {
            label: "会计期间",
            prop: "accountPeriod",
            type: "month",
            format: "yyyy-MM",
            valueFormat: "yyyy-MM",
            rules: [
              {
                required: true,
                message: "请输入会计期间",
                trigger: "blur",
              },
            ],
          },
          {
            label: "第三步",
            prop: "excelFile",
            // type: "upload",
            // drag: true,
            // loadText: "模板上传中，请稍等",
            // propsHttp: {
            //   res: "data",
            // },
            // tip: "注：导入的数据会替换当前页面数据",
            // action: "/api/blade-wages/wages/import-wages",
            span: 24,
            formslot: true,
          },
        ],
      },
      excelForm: {},
      accountPeriod1: "",
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "auto",
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        cellBtn: true,
        selection: true,
        headerAlign: "center",
        align: "center",
        dialogClickModal: false,
        column: [
          {
            label: "xx社保公积金缴费情况表",
            children: [
              {
                label: "会计期间",
                prop: "accountPeriod",
                rules: [
                  {
                    required: true,
                    message: "请输入会计期间",
                    trigger: "blur",
                  },
                ],
              },
              {
                label: "员工信息",
                children: [
                  {
                    label: "所属组织",
                    prop: "deptId",
                    search: true,
                    dicData: [],
                    type: "tree",
                    addDisabled: false,
                    width: 300,
                    props: {
                      label: "title",
                    },
                    slot: true,
                    rules: [
                      {
                        required: true,
                        message: "请输入所属组织id",
                        trigger: "blur",
                      },
                    ],
                  },
                  // {
                  //   label: "所属组织",
                  //   prop: "deptName",
                  //   rules: [{
                  //     required: true,
                  //     message: "请输入所属组织",
                  //     trigger: "blur"
                  //   }]
                  // },
                  {
                    label: "姓名",
                    prop: "name",
                    search: true,
                    rules: [
                      {
                        required: true,
                        message: "请输入姓名",
                        trigger: "blur",
                      },
                    ],
                  },
                  {
                    label: "身份证",
                    prop: "idCard",
                    width: 200,
                    rules: [
                      {
                        required: true,
                        message: "请输入身份证",
                        trigger: "blur",
                      },
                    ],
                  },
                  {
                    label: "社保公积金年份",
                    prop: "socialYear",
                    rules: [
                      {
                        required: true,
                        message: "请输入社保公积金年份",
                        trigger: "blur",
                      },
                    ],
                  },
                ],
              },
              {
                label: "社会保险",
                children: [
                  {
                    label: "养老基数",
                    prop: "pensionBase",
                    cell: true,
                    rules: [
                      {
                        required: true,
                        message: "请输入养老基数",
                        trigger: "blur",
                      },
                    ],
                  },
                  {
                    label: "医疗基数",
                    prop: "medicalBase",
                    cell: true,
                    rules: [
                      {
                        required: true,
                        message: "请输入医疗基数",
                        trigger: "blur",
                      },
                    ],
                  },

                  {
                    label: "养老",
                    children: [
                      {
                        label: "单位",
                        children: [
                          {
                            label: "16.0%",
                            prop: "pensionUnit",
                            rules: [
                              {
                                required: true,
                                message: "请输入养老-单位",
                                trigger: "blur",
                              },
                            ],
                          },
                        ],
                      },
                      {
                        label: "个人",
                        children: [
                          {
                            label: "8.0%",
                            prop: "pensionUser",
                            rules: [
                              {
                                required: true,
                                message: "请输入养老-个人",
                                trigger: "blur",
                              },
                            ],
                          },
                        ],
                      },
                    ],
                  },
                  {
                    label: "基本医疗",
                    children: [
                      {
                        label: "单位",
                        children: [
                          {
                            label: "8.0%",
                            prop: "medicalUnit",
                            rules: [
                              {
                                required: true,
                                message: "请输入基本医疗-单位",
                                trigger: "blur",
                              },
                            ],
                          },
                        ],
                      },
                      {
                        label: "个人",
                        children: [
                          {
                            label: "2.0%",
                            prop: "medicalUser",
                            rules: [
                              {
                                required: true,
                                message: "请输入基本医疗-个人",
                                trigger: "blur",
                              },
                            ],
                          },
                        ],
                      },
                    ],
                  },
                  {
                    label: "大病医疗",
                    children: [
                      {
                        label: "单位",
                        children: [
                          {
                            label: "9元",
                            prop: "seriousUnit",
                            rules: [
                              {
                                required: true,
                                message: "请输入大病医疗-单位",
                                trigger: "blur",
                              },
                            ],
                          },
                        ],
                      },
                    ],
                  },
                  {
                    label: "工伤",
                    children: [
                      {
                        label: "单位",
                        children: [
                          {
                            label: "0.2%",
                            prop: "industrialUnit",
                            rules: [
                              {
                                required: true,
                                message: "请输入大病医疗-单位",
                                trigger: "blur",
                              },
                            ],
                          },
                        ],
                      },
                    ],
                  },
                  {
                    label: "失业",
                    children: [
                      {
                        label: "单位",
                        children: [
                          {
                            label: "0.7%",
                            prop: "unemploymentUnit",
                            rules: [
                              {
                                required: true,
                                message: "请输入失业-单位",
                                trigger: "blur",
                              },
                            ],
                          },
                        ],
                      },
                      {
                        label: "个人",
                        children: [
                          {
                            label: "0.3%",
                            prop: "unemploymentUser",
                            rules: [
                              {
                                required: true,
                                message: "请输入失业-单位",
                                trigger: "blur",
                              },
                            ],
                          },
                        ],
                      },
                    ],
                  },

                  {
                    label: "社保(单位部分)",
                    prop: "socialUnit",
                    rules: [
                      {
                        required: true,
                        message: "请输入社保(单位部分)",
                        trigger: "blur",
                      },
                    ],
                  },
                  {
                    label: "社保(个人部分)",
                    prop: "socialUser",
                    rules: [
                      {
                        required: true,
                        message: "请输入社保(个人部分)",
                        trigger: "blur",
                      },
                    ],
                  },
                ],
              },
              {
                label: "住房公积金",
                children: [
                  {
                    label: "公积金缴费基数",
                    prop: "housingBase",
                    cell: true,
                    rules: [
                      {
                        required: true,
                        message: "请输入公积金缴费基数",
                        trigger: "blur",
                      },
                    ],
                  },
                  {
                    label: "公积金(实际版)",
                    children: [
                      {
                        label: "单位",
                        children: [
                          {
                            label: "10%",
                            prop: "housingRealityUnit",
                            rules: [
                              {
                                required: true,
                                message: "请输入公积金(实际版)-单位",
                                trigger: "blur",
                              },
                            ],
                          },
                        ],
                      },
                      {
                        label: "个人",
                        children: [
                          {
                            label: "6%",
                            prop: "housingRealityUser",
                            rules: [
                              {
                                required: true,
                                message: "请输入公积金(实际版)-个人",
                                trigger: "blur",
                              },
                            ],
                          },
                        ],
                      },
                    ],
                  },
                  {
                    label: "公积金(对账版)",
                    children: [
                      {
                        label: "单位",
                        children: [
                          {
                            label: "8%",
                            prop: "housingReconciliaUnit",
                            rules: [
                              {
                                required: true,
                                message: "请输入公积金(实际版)-单位",
                                trigger: "blur",
                              },
                            ],
                          },
                        ],
                      },
                      {
                        label: "个人",
                        children: [
                          {
                            label: "8%",
                            prop: "housingReconciliaUser",
                            rules: [
                              {
                                required: true,
                                message: "请输入公积金(实际版)-个人",
                                trigger: "blur",
                              },
                            ],
                          },
                        ],
                      },
                    ],
                  },
                  {
                    label: "公积金(单位部分)",
                    prop: "housingUnit",
                    rules: [
                      {
                        required: true,
                        message: "请输入公积金(单位部分)",
                        trigger: "blur",
                      },
                    ],
                  },
                  {
                    label: "公积金(个人部分)",
                    prop: "housingUser",
                    rules: [
                      {
                        required: true,
                        message: "请输入公积金(个人部分)",
                        trigger: "blur",
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: false,
        viewBtn: false,
        delBtn: this.vaildData(this.permission.socialhousing_delete, false),
        editBtn: this.vaildData(this.permission.socialhousing_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  mounted() {
    this.initData();
  },
  methods: {
    handleTemplate() {
      window.open(
        `/api/blade-wages/socialhousing/export-template?${
          this.website.tokenHeader
        }=${getToken()}`
      );
    },
    handleImport() {
      this.excelBox = true;
    },
    // 反月结
    createSettlementBox() {
      nosettlement(this.accountPeriod1).then((res) => {
        if (res.data.code == 200) {
          this.accountPeriod1 = "";
          this.nosettlementBox = false;
          this.onLoad(this.page);
        }
      });
    },
    handleNoSettlement() {
      this.accountPeriod1 = "";
      this.nosettlementBox = true;
    },
    uploadBefore(file) {
      console.log(file, 4444444);
      if (!this.excelForm.accountPeriod)
        return this.$message.error("请先选择会计期间!");
      importWages(file, this.excelForm.accountPeriod).then(() => {
        this.excelBox = false;
        this.excelForm = {};
        this.onLoad(this.page);
      });
    },
    //月结
    handleSettlement() {
      settlement().then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
        }
      );
    },
    handleExport() {
      if (this.data.length != 0) {
        window.open(
          `/api/blade-wages/socialhousing/export-socialHousing?${
            this.website.tokenHeader
          }=${getToken()}&status=0`
        );
      } else {
        this.$message.warning("当前无数据，按钮不可用");
        return;
      }
    },
    initData() {
      getDeptTree().then((res) => {
        const column = this.findObject(this.option.column, "deptId");
        column.dicData = res.data.data;
      });
    },
    rowSave(row, done, loading) {
      let obj = {
        medicalBase: row.medicalBase,
        pensionBase: row.pensionBase,
        housingBase: row.housingBase,
        idCard: row.idCard,
      };
      add(obj).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      let obj = {
        id: row.id,
        medicalBase: row.medicalBase,
        pensionBase: row.pensionBase,
        housingBase: row.housingBase,
        idCard: row.idCard,
      };
      update(obj).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      params.status = 0;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      )
        .then((res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        })
        .catch(() => {
          this.data = [];
          this.loading = false;
        });
    },
  },
};
</script>

<style scoped lang="scss">
.downTip {
  padding-top: 15px;
  font-size: 12px;
  font-family: Source Han Sans, Source Han Sans;
  font-weight: 400;
  color: #666666;
  line-height: 18px;
}
/deep/.el-dialog__header {
  text-align: center;
  font-size: 24px;
  font-family: Source Han Sans, Source Han Sans;
  font-weight: 600;
  color: #000000;
  border-bottom: 1px solid #d8d8d8;
}

/deep/.el-dialog__footer {
  width: 100%;
  display: flex;
}
/deep/.dialog-footer {
  padding-top: 15px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-evenly;
}
.label {
  font-size: 16px;
  font-family: Source Han Sans, Source Han Sans;
  font-weight: 400;
  color: #666666;
  width: 100%;
  text-align: center;
}
.date {
  padding-top: 16px;
  width: 100%;
  margin: 0 auto;
  height: 32px;
  border-radius: 2px 2px 2px 2px;
  text-align: center;
  .el-date-editor--month {
    width: 161px;
    height: 100%;
  }
}
.tip {
  padding-top: 20px;
  width: 100%;
  text-align: center;
}
</style>
